import axios from 'axios'
import {Message} from 'element-ui'
import Config from "../config.js" 

axios.interceptors.request.use(config=> {
  return config;
}, err=> {
  Message.error({message: '请求超时!'});
  return Promise.resolve(err);
})
axios.interceptors.response.use(data=> {
  if (data.status && data.status == 200 && data.data.status == 'error') {
    Message.error({message: data.data.msg});
    return;
  }
  return data;
}, err=> {
  if (err.response.status == 504||err.response.status == 404) {
    Message.error({message: '服务器被吃了⊙﹏⊙∥'});
  } else if (err.response.status == 403) {
    Message.error({message: '权限不足,请联系管理员!'});
  } else if(err.response.status == 500){
    Message.error({message: '服务器异常，请联系维护人员!'});
  } else if (err.response.status == 401) {
    Message.error({message: '登录超时，请重新登录'});
    // TODO 跳转到登录界面
  }else {
    if(err.response.data.msg){
      Message.error({message: err.response.data.msg});
    }else {
      Message.error({message: '未知错误!'});
    }
  }
  return Promise.resolve(err);
})

let base = Config.production? Config.baseURL : "";//项目的基础URL

const postRequest = (url, params) => {
  return axios({
    method: 'post',
    url: `${base}${url}`,
    data: params,
    // transformRequest: [function (data) {
    //   let ret = ''
    //   for (let it in data) {
    //     ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
    //   }
    //   return ret
    // }],
    headers: {
      // 'Content-Type': 'application/x-www-form-urlencoded'
      'Content-Type': 'application/json;charset=utf-8'
      
    }
  });
}
const uploadFileRequest = (url, params) => {
  return axios({
    method: 'post',
    url: `${base}${url}`,
    data: params,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
}
const loginPostRequest = (url, params) => {
  return axios({
    method: 'post',
    url: `${base}${url}`,
    data: params,
    transformRequest: [function (data) {
      let ret = ''
      for (let it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
      }
      return ret
    }],
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
      // 'Content-Type': 'application/json;charset=utf-8'
      
    }
  });
}
const exportRequest = (url,params) => {
  return axios({
    method: 'post',
    url: `${base}${url}`,
    data: params,
    transformRequest: [function (data) {
      let ret = ''
      for (let it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
      }
      return ret
    }],
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
      // 'Content-Type': 'application/json;charset=utf-8'
      
    },
    responseType: 'blob'
  });  
}
const putRequest = (url, params) => {
  // return axios({
  //   method: 'put',
  //   url: `${base}${url}`,
  //   data: params,
  //   transformRequest: [function (data) {
  //     let ret = ''
  //     for (let it in data) {
  //       ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
  //     }
  //     return ret
  //   }],
  //   headers: {
  //     'Content-Type': 'application/x-www-form-urlencoded'
  //   }
  // });
   return axios({
    method: 'put',
    url: `${base}${url}`,
    data: params,
    headers: {
      // 'Content-Type': 'application/x-www-form-urlencoded'
      'Content-Type': 'application/json;charset=utf-8'
    }
  });
}
const deleteRequest = (url) => {
  return axios({
    method: 'delete',
    url: `${base}${url}`
  });
}
const getRequest = (url,params) => {
  // return axios({
  //   method: 'get',
  //   url: `${base}${url}`
  // });
  return axios({
    method: 'get',
    url: `${base}${url}`,
    params:params,
    headers: {
      'Content-Type': 'application/json;charset=utf-8'
    }
  });
}

export default {getRequest, postRequest, loginPostRequest, exportRequest, deleteRequest, putRequest, uploadFileRequest};